package com.church.services;

import java.util.Date;
import java.util.List;

import com.church.dao.QandaDao;
import com.church.db.Qanda;

public class QandaServicesImpl implements QandaServices {
	private QandaDao qandaDao; 
	private String subject;
	public QandaDao getQandaDao() {
		return qandaDao;
	}

	public void setQandaDao(QandaDao qandaDao) {
		this.qandaDao = qandaDao;
	}

	
	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	@Override
	public String quest(String subject, String userName, String quest) {
		Qanda qanda=new Qanda();
		qanda.setQuest(quest);
		qanda.setSubject(subject);
		qanda.setUserName(userName);
		Date datetime = new Date(System.currentTimeMillis());
		qanda.setQuestTime(datetime);
		qanda.setState("0");
		return qandaDao.add(qanda);
	}

	@Override
	public String answer(long id, String teacher, String answer) {
		Qanda qanda=qandaDao.findById(id);
		if(qanda!=null)
		{
			qanda.setTeacher(teacher);
			Date datetime = new Date(System.currentTimeMillis());
			qanda.setAnswerTime(datetime);
			qanda.setAnswer(answer);
			qanda.setState("1");
			qandaDao.update(qanda);
			return "success";
		}
		return "error";
	}
	@Override
	public String deleteById(long id) {
		Qanda qanda=qandaDao.findById(id);
		if(qanda!=null)
		{
			qanda.setState("3");
			qandaDao.update(qanda);
			return "success";
		}
		return "error";
	}

	@Override
	public List<Qanda> getQandaBySubject(String subject) {
		return qandaDao.getList(subject);
	}

}
